<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: 16 条</span>
        </template>
        <template #right>
          <el-button
            type="warning"
            size="small"
            @click="$router.push('/employees/import')"
          >excel导入</el-button>
          <el-button
            type="danger"
            size="small"
            @click="empPortXlsx"
          >excel导出</el-button>
          <el-button
            type="primary"
            size="small"
            @click="showDialog = true"
          >新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像" width="170px">
            <template slot-scope="scope">
              <ImageHolder :src="scope.row.staffPhoto" />
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="mobile" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template slot-scope="scope">
              {{ geshihua(scope.row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable />
          <el-table-column label="操作" width="280">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                @click="$router.push('/employees/detail?id=' + scope.row.id)"
              >查看</el-button>
              <el-button
                type="text"
                size="small"
                @click="hAssignRole(scope.row.id)"
              >分配角色</el-button>
              <el-button
                type="text"
                size="small"
                @click="hDel(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <!-- 分页组件 -->
          <el-pagination
            :current-page="pageName.page"
            layout="total, sizes,prev, pager, next, jumper"
            :total="total"
            :page-size="pageName.size"
            :page-sizes="[2, 5, 10]"
            @current-change="hCurrentChange"
            @size-change="hSizeChange"
          />
        </el-row>
      </el-card>
    </div>

    <el-dialog
      title="新增员工"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="resetDialog"
    >
      <emp-dialog ref="refEmpdialog" @success="success" />
    </el-dialog>

    <el-dialog
      :visible.sync="showDialogRole"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="分配角色"
    >
      <assign-role v-if="showDialogRole" :cur-id="curId" @success="hSuccess" />
    </el-dialog>
  </div>
</template>

<script>
import { LIST_MAP } from '@/consted/const'
import { getEmployeeList, delEmployee } from '@/api/employees'
import EmpDialog from './empDialog.vue'
import AssignRole from './assignRole.vue'

export default {
  components: { EmpDialog, AssignRole },
  data() {
    return {
      list: [],
      pageName: {
        page: 1,
        size: 5
      },
      total: null,
      showDialog: false,
      showDialogRole: false,
      curId: ''
    }
  },
  created() {
    this.getEmployee()
  },
  methods: {
    hSuccess() {
      this.showDialogRole = false
    },
    hAssignRole(id) {
      this.showDialogRole = true
      this.curId = id
    },
    formatData(list) {
      const map = {
        id: '编号',
        password: '密码',
        mobile: '手机号',
        username: '姓名',
        timeOfEntry: '入职日期',
        formOfEmployment: '聘用形式',
        correctionTime: '转正日期',
        workNumber: '工号',
        departmentName: '部门',
        staffPhoto: '头像地址'
      }
      const header = Object.values(map)
      const data = list.map((item) => {
        item.formOfEmployment = LIST_MAP[item.formOfEmployment]
        return Object.values(item)
      })
      return { header, data }
    },
    empPortXlsx() {
      import('@/vendor/Export2Excel').then(async(excel) => {
        // excel表示导入的模块对象
        console.log(excel)
        const res = await getEmployeeList(this.pageName)
        const { header, data } = this.formatData(res.data.rows)
        console.log(header, data)
        excel.export_json_to_excel({
          header, // 表头 必填
          data, // 具体数据 必填
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    resetDialog() {
      this.$refs.refEmpdialog.resetForm()
    },
    success() {
      this.showDialog = false
      this.total++
      this.pageName.page = Math.ceil(this.total / this.pageName.size)
      this.getEmployee()
    },
    async getEmployee() {
      try {
        const res = await getEmployeeList(this.pageName)
        this.list = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.dir(error)
      }
    },
    // 处理格式化聘用形式
    geshihua(num) {
      return LIST_MAP[num] || '未知'
    },
    // 页码
    hCurrentChange(val) {
      //   console.log(val)
      this.pageName.page = val
      this.getEmployee()
    },
    // 一页显示多少条数据
    hSizeChange(val) {
      //   console.log(val)
      this.pageName.size = val
      this.pageName.page = 1
      this.getEmployee()
    },
    // 删除功能
    hDel(id) {
      //   console.log(id)
      this.$confirm('此操作将永久删除，是否继续？', '提示', {
        cancelButtonText: '取消',
        confirmButtonText: '确认',
        type: 'warning'
      })
        .then(() => {
          this.doDel(id)
          console.log(1)
        })
        .catch(() => {})
    },
    // 删除功能
    async doDel(id) {
      try {
        const res = await delEmployee(id)
        this.$message.success(res.message)
        if (this.list.length === 1 && this.pageName.page > 1) {
          this.pageName.page--
        }
        this.getEmployee()
      } catch (error) {
        this.$message.warning(error)
      }
    }
  }
}
</script>

<style scoped>
/deep/img {
  width: 150px;
  height: 150px;
  border-radius: 50%;
}
</style>
